﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;

namespace ConvertFr3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("1.批量转换：输入转换目录");
            Console.WriteLine("2.单个转换：输入转换文件");
            string inputOrder = Console.ReadLine();
            if (inputOrder == "1")
            {
                string dirPath = Console.ReadLine();
                if (Directory.Exists(dirPath))
                {
                    string[] filePaths = Directory.GetFiles(dirPath, "*.fr3");
                    if (filePaths.Length > 0)
                    {
                        for (int i = 0; i < filePaths.Length; i++)
                        {
                            Convert(filePaths[i]);
                            Console.WriteLine("[{0}/{1}]：转换完成", i + 1, filePaths.Length);
                        }

                        Console.WriteLine("全部转换完成");
                    }
                    else
                    {
                        Console.WriteLine("文件夹中不存在fr3文件");
                    }
                }
                else
                {
                    Console.WriteLine("文件夹不存在");
                }
            }
            else if (inputOrder == "2")
            {
                string filePath = Console.ReadLine();
                Convert(filePath);
                Console.WriteLine("转换完成");
            }

            Console.WriteLine("按下任意键结束...");
            Console.ReadKey();
        }

        static void Convert(string filePath)
        {
            string rf3 = ReadRf3(filePath);
            rf3 = ReplaceTrf(rf3);
            rf3 = ReplaceMemoText(rf3);
            rf3 = ReplaceSpaceWord(rf3);
            rf3 = ReplaceChineseWord(rf3);
            rf3 = ReplaceTfrxRoundRectView(rf3);
            rf3 = ReplaceDateTime(rf3);
            SaveFile(filePath, rf3);
        }

        static string ReadRf3(string filePath)
        {
            string rf3 = File.ReadAllText(filePath, Encoding.Default);
            return rf3;
        }

        static string ReplaceTrf(string rf3)
        {
            rf3 = rf3.Replace("Tfr", "Tfrx");
            return rf3;
        }

        static string ReplaceMemoText(string rf3)
        {
            rf3 = rf3.Replace("Memo.text", "Text");
            return rf3;
        }

        static string ReplaceSpaceWord(string rf3)
        {
            rf3 = rf3.Replace("#13;&#10;", string.Empty);
            return rf3;
        }

        static string ReplaceChineseWord(string rf3)
        {
            rf3 = rf3.Replace("分组头", "TfrxGroupHeader");
            rf3 = rf3.Replace("主项脚", "TfrxFooter");
            return rf3;
        }

        static string ReplaceTfrxRoundRectView(string rf3)
        {
            rf3 = rf3.Replace("TfrxRoundRectView", "TfrxShapeView");
            return rf3;
        }

        static string ReplaceDateTime(string rf3)
        {
            string regexStr = "ReportOptions.CreateDate=\"\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+\"";
            Regex rep_regex = new Regex(regexStr);
            rf3 = rep_regex.Replace(rf3, " ReportOptions.CreateDate=\"41593.7170264005\"");

            regexStr = "ReportOptions.LastChange=\"\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+\"";
            rep_regex = new Regex(regexStr);
            rf3 = rep_regex.Replace(rf3, " ReportOptions.LastChange=\"41593.7170264005\"");
            return rf3;
        }

        static void SaveFile(string filePath, string rf3)
        {
            string dir = Path.GetDirectoryName(filePath) + "\\转换后的fr3文件";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            string fileName = Path.GetFileName(filePath);
            string savePath = Path.Combine(dir, fileName);
            File.WriteAllText(savePath, rf3, Encoding.UTF8);
        }
    }
}
